package com.xubeizhi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xubeizhi.common.PageBean;
import com.xubeizhi.common.Result;
import com.xubeizhi.entity.Product;
import com.xubeizhi.mapper.ProductMapper;
import com.xubeizhi.service.ProductService;
import com.xubeizhi.utils.DataUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    /**
     * 新增分类
     * @return
     */
    @Override
    public Result createProduct(Product product) {
        BeanUtils.copyProperties(Product.class,product);

        product.setCreateTime(DataUtils.getDateNow());
        product.setUpdateTime(DataUtils.getDateNow());
        Integer categoryId = product.getCategoryId();
        if (categoryId == null){
            return Result.success(1,"分类id为空",null);
        }
        int rows = baseMapper.insert(product);

        return rows > 0 ? Result.success(0,"操作成功",product) : Result.error(1,"操作失败",null);
    }

    /**
     * 下架商品
     * @param product
     * @return
     */
    @Override
    public Result soldProduct(Product product) {
        product.setStatus("1");
        product.setSoldTime(DataUtils.getDateNow());
        Integer rows = baseMapper.soldProductStatus(product);
        return rows > 0 ? Result.success(0,"操作成功",product) : Result.error(1,"操作失败",null);
    }

    /**
     * 修改分类
     * @param product
     * @return
     */
    @Override
    public Result updateProductById(Product product) {
        BeanUtils.copyProperties(Product.class,product);
        product.setUpdateTime(DataUtils.getDateNow());
        int rows = baseMapper.updateById(product);
        return rows > 0 ? Result.success(0,"操作成功",product) : Result.error(1,"操作失败",null);
    }

    /**
     * 分页查询
     * @param pageNum 页面数据总和
     * @param pageSize 当前页
     * @param status
     * @param removeStatus
     * @return
     */
    @Override
    public Result<PageBean<Product>> listProduct(Integer pageNum,Integer pageSize, String status, String removeStatus) {
        PageBean pb = new PageBean<>();

        PageHelper.startPage(pageNum,pageSize);

        List<Product> products = baseMapper.listProduct(status, removeStatus);

        Page<Product> page = (Page<Product>) products;
        pb.setTotal(pb.getTotal());
        pb.setItems(page.getResult());

        return Result.success(0,"查询成功",products);
    }

    /**
     * 按条件搜索分类
     * @param productName
     * @param status
     * @param removeStatus
     * @return
     */
    @Override
    public Result<PageBean<Product>> searchProduct(String productName, String status, String removeStatus) {
//        PageBean pb = new PageBean<>();

        List<Product> products = baseMapper.selectProductList(productName, status, removeStatus);
//        Page<Product> page = (Page<Product>) products;
//        pb.setTotal(pb.getTotal());
//        pb.setItems(page.getResult());

        return Result.success(0,"查询成功",products);
    }

    /**
     * 删除产品 只修改remove_status状态
     * @param product
     * @return
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Result removeProduct(Product product) {
        product.setUpdateTime(DataUtils.getDateNow());
        Integer rows = baseMapper.removeProduct(product.getId());
        if (rows != null){
            return Result.success(0,"修改成功",product);
        }
        return Result.error(1,"修改失败",null);
    }
}
